package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.views.RecordCourseStudent;
import com.xmy.cultivate.respons.ResSchoolDate;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 排课详情 Mapper 接口
 * </p>
 *
 * @author min
 *
 * @since 2022-08-01
 */
public interface CourseSchedulingDetailMapper extends BaseMapper<CourseSchedulingDetail> {

    @Select("select a.*,b.course_id from course_scheduling_detail as a LEFT JOIN grade as b on a.grade_id = b.id  where a.id = #{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "teacherObj",
                    column = "teacher_id",
                    one = @One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(
                    property = "assistantObj",
                    column = "assistant_id",
                    one = @One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(
                    property = "classRoomObj",
                    column = "class_room_id",
                    one = @One(select = "com.xmy.cultivate.mapper.ClassRoomMapper.selectById")
            ),
            @Result(
                    property = "subjectsObj",
                    column = "subjects_id",
                    one = @One(select = "com.xmy.cultivate.mapper.SubjectsMapper.selectById")
            ),
            @Result(
                    property = "gradeObj",
                    column = "grade_id",
                    javaType = Grade.class,
                    one = @One(select = "com.xmy.cultivate.mapper.GradeMapper.selectById")
            ),
            @Result(
                    property = "courseObj",
                    column = "course_id",
                    javaType = Grade.class,
                    one = @One(select = "com.xmy.cultivate.mapper.CourseMapper.selectById")
            )
    })
    public abstract CourseSchedulingDetail getOneById(@Param("id") Long id);

    @Select("SELECT * from student ${ew.customSqlSegment}")
    public abstract List<RecordCourseStudent> recordStudentList(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select * from (select a.*,c.year_class_id from course_scheduling_detail as a LEFT JOIN grade as b on a.grade_id = b.id LEFT JOIN course as c on b.course_id = c.id) as t ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "teacherObj",
                    column = "teacher_id",
                    one = @One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(
                    property = "classRoomObj",
                    column = "class_room_id",
                    one = @One(select = "com.xmy.cultivate.mapper.ClassRoomMapper.selectById")
            ),
            @Result(
                    property = "subjectsObj",
                    column = "subjects_id",
                    one = @One(select = "com.xmy.cultivate.mapper.SubjectsMapper.selectById")
            ),
            @Result(
                    property = "gradeObj",
                    column = "grade_id",
                    one = @One(select = "com.xmy.cultivate.mapper.GradeMapper.selectById")
            ),
            @Result(
                    property = "schoolObj",
                    column = "schoolId",
                    one = @One(select = "com.xmy.cultivate.mapper.Organization.selectById")
            ),
            @Result(
                    property = "courseObj",
                    column = "course_id",
                    javaType = Grade.class,
                    one = @One(select = "com.xmy.cultivate.mapper.CourseMapper.selectById")
            )
    })
    public abstract List<CourseSchedulingDetail> findAll(@Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("select * from (select a.*,c.year_class_id from course_scheduling_detail as a LEFT JOIN grade as b on a.grade_id = b.id LEFT JOIN course as c on b.course_id = c.id) as t ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "teacherObj",
                    column = "teacher_id",
                    one = @One(select = "com.xmy.cultivate.mapper.StaffMapper.selectById")
            ),
            @Result(
                    property = "classRoomObj",
                    column = "class_room_id",
                    one = @One(select = "com.xmy.cultivate.mapper.ClassRoomMapper.selectById")
            ),
            @Result(
                    property = "subjectsObj",
                    column = "subjects_id",
                    one = @One(select = "com.xmy.cultivate.mapper.SubjectsMapper.selectById")
            ),
            @Result(
                    property = "gradeObj",
                    column = "grade_id",
                    one = @One(select = "com.xmy.cultivate.mapper.GradeMapper.selectById")
            ),
            @Result(
                    property = "schoolObj",
                    column = "schoolId",
                    one = @One(select = "com.xmy.cultivate.mapper.Organization.selectById")
            ),
            @Result(
                    property = "courseObj",
                    column = "course_id",
                    javaType = Grade.class,
                    one = @One(select = "com.xmy.cultivate.mapper.CourseMapper.selectById")
            )
    })
    public abstract IPage<CourseSchedulingDetail> findAllForPage(IPage<?> page,@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select({"<script>",
            "select * from scheduling_union_repair",
            "where",
            "scheduling_type = 1 and course_type in(1,4)",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
            "and school_id = #{schoolId}",
            "</when>",
            "<when test='gradeId!=null and gradeId!=&apos;&apos; '>",
            "and grade_id = #{gradeId}",
            "</when>",
            "<when test='teacherId!=null and teacherId!=&apos;&apos; '>",
            "and teacher_id = #{teacherId}",
            "</when>",
            "<when test='schoolDate!=null and schoolDate!=&apos;&apos; '>",
            "and school_date BETWEEN #{schoolDate} AND #{endSchoolDate}",
            "</when>",
            "<when test='lessonType!=null and lessonType!=&apos;&apos; '>",
            "and lesson_type = #{lessonType}",
            "</when>",
            "GROUP BY time_start,time_end",
            "ORDER BY time_start,time_end",
            "</script>"}
    )
    public List<ResSchoolDate> getSchoolDateList(@Param("schoolId") String schoolId, @Param("gradeId") String gradeId, @Param("teacherId") String teacherId, @Param("schoolDate") String schoolDate, @Param("endSchoolDate") String endSchoolDate, @Param("lessonType") String lessonType);

    @Select("select * from course_scheduling_detail where id = ${id}")
    public CourseSchedulingDetail getCourseSchedulingDetailById(@Param("id") Long id);

    @Select("select * from (select t1.*,t2.`status` as grade_status from course_scheduling_detail as t1 LEFT JOIN grade as t2 on t1.grade_id = t2.id) as t ${ew.customSqlSegment}")
    public List<CourseSchedulingDetail> getCourseSchedulingDetailList(@Param(Constants.WRAPPER) Wrapper wrapper);

}
